let Mock =require("mockjs")
let data = [
    {id:0,name:"苹果",text:"好吃的水果"},

]
Mock.mock("/api/homeorder","get",function(config){
    console.log(config)
    return data
})
Mock.mock("/api/homeorder","post",function(config){
    let obj = JSON.parse(config.body)
    let name = obj.name
    let text = obj.text
    data.push({id:data.length,name,text})
    console.log(data)
    // if(data.includes(name)){
    //     return data;
    // }
    return data
})
// 修改数据
Mock.mock(/\/api\/homeorder\/edit\/\d/,"put",function(config){
    console.log(config)
    let arr=config.url.split("/")
    let id = arr.pop()
    let canshu=config.body 
    canshu =JSON.parse(canshu)
    data[id].name=canshu.name
    data[id].text=canshu.text
    return data
})
// 删除数据
Mock.mock(/\/api\/homeorder\/\d/,"delete",function(config){
    console.log(config.url)
    let arr= config.url.split("/")
    let id= arr.pop()
    data.splice(id,1)
    data=data.map(function(item,index){
        return {
            id:index,
            name:item.name,
            text:item.text
        }
    })
    return data
})